from dbfunction import Databasefunction
db = Databasefunction()
#from util import read_userinfo
#查询
class Busines
    def query():
        from user import id
        sql = 'select ubalance from woniu atm where id = {} '.format(id)
        db.select(sql)

        '''
        from user import login_user_index
    #利用全局变量,取出之前保存的用户下标
        user_list = read_userinfo()
    #读取下标对应的客户金额
        print(user_list[login_user_index].split(",")[2])
        '''
        from menu import main_menu
        main_menu()


    #取款
    def withdraw():
        from user import uid
        '''
        from user import login_user_index
        '''
        money = int(input('请输入取款金额'))
        if money < 0:
            print('请输入正确金额')
            withdraw()
        elif check_balance(uid,money):
            print('取款{}'.format(money))
            change_balance(uid,money*-1)
            from menu import main_menu
            main_menu()
        else:
            print('余额不足,谢谢')
        from menu import main_menu
        main_menu




    def check_balance(uid,money):
        sql = 'select ubalance from woniuatm where uid={}'.format(uid)
        db.select(sql)
        if ubalance < money:
        '''
        user_list = read_userinfo()
        if int(user_list[user_index].split(',')[2]) < money:
        '''
            return False
        else:
            return True

    def change_balance(uid,money):
        sql = 'select ubalance from woniuatm where uid ={}'.format(uid)
        db.select(sql)

        '''
        user_list = read_userinfo()
        userinfo = user_list[user_index].split(',')
        uname = userinfo[0]
        upwd = userinfo[1]
        ubalance = int(userinfo[2])
        '''
        ubalance += money
        sql = 'update from woniuatm set ubalance={} where uid={}'.format(ubalance,uid)
        db.update(sql)
        '''
        userinfo = uname+","+upwd+","+str(ubalance)+"\n"
        user_list[user_index] = userinfo
        from util import userinfo_path
        with open(file = userinfo_path,mode="w",encoding="utf-8") as file:
            file.writelines(user_list)
        '''

    def deposit():
        money = int(input("请输入要存款的金额"))
        if money < 0 :
            print("请输入正确的金额")
            deposit()
        else:

            from user import uid

            change_balance(uid,money)
            from menu import main_menu
            main_menu()

    def transfer():
        uname = input("请输入收款方账户名称")
        from user import check_uname
        sql = 'select uid form woniuatm where uname = '{}''.format(uname)
        db.select()
        to_id = uid
        if to_index:
            money = int(input("请输入转账金额"))
            from user import uid
            if check_balance(uid,money):
                change_balance(uid,money*-1)
                change_balance(to_id,money)
                from menu import main_menu
                main_menu()
            else:
                print("金额不足,不可以转账,请先存款")
                deposit()

        else:
            print("对方账户不存在")
            transfer()